<?php

declare(strict_types=1);

namespace App\Box\Model;

use App\Lib\AdminEnum;
use Mine\MineModel;

/**
 * @property int $id ID
 * @property int $category_id 盒子分类id
 * @property string $name 盒子名称
 * @property int $price 盒子价格
 * @property string $img 盒子背景图
 * @property int $box_style_id 盒子风格id
 * @property int $sort 排序
 * @property int $mode_type 1:变价模式 2：变物模式 3：活动箱子
 * @property string $price_coefficient 售价系数
 * @property int $activity_num 每日活动开箱次数
 * @property int $activity_price 每日活动充值金额
 * @property int $activity_start 活动开始时间
 * @property int $activity_end 活动结束时间
 * @property int $status 是否展示的箱子(1展示，2:隐藏）
 * @property int $created_by 创建者
 * @property int $create_time 创建时间
 * @property int $update_time 更新时间
 * @property int $delete_time 删除
 */
class BoxInfo extends MineModel
{
    const MODE_TYPE_PRICE=1;  //变价
    const MODE_TYPE_GOODS=2; //变物
    const MODE_TYPE_ACTIVITY=3; //活动
    protected ?string $dateFormat = 'U';
    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'update_time';
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'box_info';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['id', 'category_id', 'name', 'price', 'img', 'box_style_id', 'sort', 'mode_type', 'price_coefficient', 'activity_num', 'activity_price', 'activity_start', 'activity_end', 'status', 'created_by', 'create_time', 'update_time', 'delete_time'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'category_id' => 'integer', 'price' => 'integer', 'box_style_id' => 'integer', 'sort' => 'integer', 'mode_type' => 'integer', 'price_coefficient' => 'decimal:2', 'activity_num' => 'integer', 'activity_price' => 'integer', 'activity_start' => 'integer', 'activity_end' => 'integer', 'status' => 'integer', 'created_by' => 'integer', 'create_time' => 'integer', 'update_time' => 'integer', 'delete_time' => 'integer'];
    public function category(){
        return $this->hasOne(BoxCategory::class,'id','category_id');
    }
    public function stype(){
        return $this->hasOne(BoxStyle::class,'id','box_style_id');
    }
    public function getPriceAttribute($value){
        return number_format($value/AdminEnum::PRICE_MULTIPLE,2,'.','');
    }


    public function setPriceAttribute($value){
        $this->attributes['price'] = (int)($value * AdminEnum::PRICE_MULTIPLE);
    }
    public function setActivityPriceAttribute($value){
        $this->attributes['activity_price'] = (int)($value * AdminEnum::PRICE_MULTIPLE);
    }
    public function getActivityPriceAttribute($value){
        return number_format($value/AdminEnum::PRICE_MULTIPLE,2,'.','');
    }
    public function getActivityStartAttribute($value)
    {
        return empty($value) ? '' : date('Y-m-d H:i:s', (int)$value);
    }

    public function setActivityStartAttribute($value)
    {
        $this->attributes['activity_start'] = empty($value) ? 0 : strtotime($value);
    }

    public function getActivityEndAttribute($value)
    {
        return empty($value) ? '' : date('Y-m-d H:i:s', (int)$value);
    }

    public function setActivityEndAttribute($value)
    {
        $this->attributes['activity_end'] = empty($value) ? 0 : strtotime($value);
    }

}
